package com.sas.hnn;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAdapter {

	public static final String KEY_FIRST = "first";
	public static final String KEY_SECOND = "second";
	public static final String KEY_ROWID = "_id";

	private static final String TAG = DbAdapter.class.getSimpleName();
	/**
	 * @uml.property  name="mDbHelper"
	 * @uml.associationEnd  
	 */
	private DatabaseHelper mDbHelper;
	/**
	 * @uml.property  name="mDb"
	 * @uml.associationEnd  
	 */
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "data";
	private static final String DATABASE_TABLE = "waste";
	private static final int DATABASE_VERSION = 2;
	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (_id integer primary key autoincrement, "
			+ KEY_FIRST + " varchar(32), " + KEY_SECOND
			+ " varchar(32));";

	/**
	 * @uml.property  name="mCtx"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.d(TAG, "in onCreate()");
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.d(TAG, "in onUpgrade()");
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

		@Override
		public synchronized void close() {
			// TODO Auto-generated method stub
			super.close();
		}
		
	}

	/**
	 * Constructor - takes the context to allow the database to be
	 * opened/created
	 * 
	 * @param ctx
	 *            the Context within which to work
	 */
	public DbAdapter(Context ctx) {
		Log.d(TAG, "im Konstruktor()");
		this.mCtx = ctx;
	}

	/**
	 * Open the notes database. If it cannot be opened, try to create a new
	 * instance of the database. If it cannot be created, throw an exception to
	 * signal the failure
	 * 
	 * @return this (self reference, allowing this to be chained in an
	 *         initialization call)
	 * @throws SQLException
	 *             if the database could be neither opened or created
	 */
	public DbAdapter open() throws SQLException {
		Log.d(TAG, "in open()");
		mDbHelper = new DatabaseHelper(mCtx);
		if(mDb==null)
			mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		Log.d(TAG, "in close()");
		mDbHelper.close();
	}

	/**
	 * Create a new note using the title and body provided. If the note is
	 * successfully created return the new rowId for that note, otherwise return
	 * a -1 to indicate failure.
	 * 
	 * @param title
	 *            the title of the note
	 * @param body
	 *            the body of the note
	 * @return rowId or -1 if failed
	 */
	public long createWaste(String first, String second) {
		//Log.d(TAG, "in createWahl()");
		long rowId=-1;
		try {
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_FIRST, first);
			initialValues.put(KEY_SECOND, second);

			rowId = mDb.insert(DATABASE_TABLE, null, initialValues);

		} catch (SQLiteException e) {
			//Log.d(TAG, "createNote() - insert(): ", e);
		} finally {
			Log.d(TAG, "rowId: " + rowId);
		}
		return rowId;
	}

	/**
	 * Delete the note with the given rowId
	 * 
	 * @param rowId
	 *            id of note to delete
	 * @return true if deleted, false otherwise
	 */
	public boolean deleteWaste(long rowId) {
		Log.d(TAG, "in deleteWahl()");
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all notes in the database
	 * 
	 * @return Cursor over all notes
	 */
	public Cursor fetchAllWaste() {
		Log.d(TAG, "in fetchAllWahlen()");
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_FIRST, KEY_SECOND }, null, null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the note that matches the given rowId
	 * 
	 * @param rowId
	 *            id of note to retrieve
	 * @return Cursor positioned to matching note, if found
	 * @throws SQLException
	 *             if note could not be found/retrieved
	 */
	public Cursor fetchWaste(long rowId) throws SQLException {
		Log.d(TAG, "in fetchWahl()");
		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_FIRST, KEY_SECOND }, KEY_ROWID + "=" + rowId, null,
				null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}
}
